package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.forms.ProjectSearchForm;
import com.scalar.forecast.projectSetup.ProjectSearch;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.commons.Constants;
import com.scalar.forecast.columnDisplay.dao.SearchMetaDataDAO;
import com.scalar.forecast.columnDisplay.SearchMetaDataComparator;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;

/**
 * Process the request for displaying the project details based on the search criteria
 *
 * Created by Veeru. 
 * Date: Feb 10, 2005
 * Time: 4:29:11 PM 
 */

public class ProjectSearchCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "projectSearch" );

        ProjectSearchForm projectSearchForm = ( ProjectSearchForm )form;
        ProjectSearch projectSearch = projectSearchForm.getProjectSearch();
        HttpSession session = request.getSession();
        HashMap errors = new HashMap ();
        request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
        User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY );
        String userId = pUser.getUserId();
        try {
            ArrayList projectsList = ProjectSetupDAO.loadProjectsList( null, projectSearch );
            if ( projectsList.size() > 0 ) {
                request.setAttribute( "showList", "true" );
                projectSearchForm.setProjectsList( projectsList );

                if ( !SearchMetaDataDAO.isUserHasEntries ( Constants.PROJECT_SETUP_TABLE_NAME, userId ) )
                {
                    SearchMetaDataDAO.saveEntriesWithUserId ( null, Constants.PROJECT_SETUP_TABLE_NAME, userId );
                }
                ArrayList columnMetaDataList = SearchMetaDataDAO.loadMetaData( null, Constants.PROJECT_SETUP_TABLE_NAME, userId );

                projectSearchForm.setColumnMetaDataList( columnMetaDataList );
                Collections.sort( columnMetaDataList, new SearchMetaDataComparator() );

                session.setAttribute( "columnMetaDataList", columnMetaDataList );
                session.setAttribute( "projects", projectsList );
                session.setAttribute( SessionKeys.REQUEST_TOTAL_RECS, projectsList.size()+"" );
                session.setAttribute( SessionKeys.REQUEST_PAGE_NUMBER, "1" );

            } else {
                errors.put( "Projects could not be found for the given search criteria", null);

            }
        } catch ( Exception e ) {
            errors.put( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not load poject details");
            errors.put( SessionKeys.ERROR_REPORT, e );
        }
        return actionForward;
    }
}
